package de.hdu.pms.dao;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.hdu.pms.model.Mitarbeiter;
import de.hdu.pms.model.Urlaub;

public class UrlaubDao extends HibernateDaoSupport {

	public static Logger Log = Logger.getLogger(UrlaubDao.class);

	public static void setLog(Logger log) {
		Log = log;
	}

	/**
	 * searches for an specific entry in the table Urlaub by primary key
	 * urlaub_id (primary key) returns all entries from the table Urlaub with a
	 * special id
	 * 
	 * @param urlaub_id
	 * @return Urlaub
	 * @author BBV
	 */
	public Urlaub findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Urlaub) template.get(Urlaub.class, id);
	}

	/**
	 * lists all entries from the table Urlaub
	 * 
	 * @returns a list of all entries from the table Urlaub
	 * @see Urlaub
	 * @author BBV
	 */
	public List<Urlaub> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(Urlaub.class);
	}

	/**
	 * lists all entries from the table Urlaub of the registered user
	 * 
	 * @returns a list of all entries from the table Urlaub, in which the
	 *          boolean-field Genehmigt or Abgelehnt is false
	 * @see Urlaub
	 * @author BBV
	 */
	
	@SuppressWarnings("unchecked")
	public List<Urlaub> findByMitarbeiter(Integer mit_id) {
		HibernateTemplate template = getHibernateTemplate();
		List<Urlaub> results = template.find("from Urlaub u where u.mitarbeiter.id = "
				+ mit_id);
		return results;
	}

	/**
	 * lists all entries from the table Urlaub, in which the boolean-field
	 * Genehmigt or Abgelehnt is false
	 * 
	 * @returns a list of all entries from the table Urlaub, in which the
	 *          boolean-field Genehmigt or Abgelehnt is false
	 * @see Urlaub
	 * @author BBV
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Urlaub> findUnchecked() {
		HibernateTemplate template = getHibernateTemplate();
		return template.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Urlaub.class);
				criteria.add(Restrictions.isNull("urlaub_genehmigt"));
				List<Urlaub> alleUrlaubstage = criteria.list();
				return alleUrlaubstage;
			}
		});

	}

	/**
	 * permit an specific entry into the Table Urlaub
	 * 
	 * @return Urlaub
	 * @param Urlaub
	 * @author BBV
	 */
	public Urlaub permit(int id) {
		HibernateTemplate template = getHibernateTemplate();
		Urlaub u = (Urlaub) template.get(Urlaub.class, id);
		u.setUrlaub_genehmigt(true);
		template.saveOrUpdate(u);
		return u;
	}

	/**
	 * defeat an specific entry into the Table Urlaub
	 * 
	 * @return Urlaub
	 * @param Urlaub
	 * @author BBV
	 */
	public Urlaub defeat(int id) {
		HibernateTemplate template = getHibernateTemplate();
		Urlaub u = (Urlaub) template.get(Urlaub.class, id);
		u.setUrlaub_genehmigt(false);
		template.saveOrUpdate(u);
		return u;
	}

	/**
	 * saves an specific entry into the Table Urlaub
	 * 
	 * @return Urlaub
	 * @param Urlaub
	 * @author BBV
	 */
	public Urlaub save(Urlaub urlaub, Integer id) {
		HibernateTemplate template = getHibernateTemplate();
		Mitarbeiter m = (Mitarbeiter) template.get(Mitarbeiter.class, id);
		urlaub.setMitarbeiter(m);
		template.saveOrUpdate(urlaub);
		return urlaub;
	}

	/**
	 * deletes an specific entry from the Table Urlaub
	 * 
	 * @param Urlaub
	 * @author BBV
	 */
	public void delete(Urlaub urlaub) {
		HibernateTemplate template = getHibernateTemplate();
		template.delete(urlaub);
	}
	
	
	/**
	 * Checks if a Date is marked as Urlaub
	 * @param datum
	 * @param mit_id
	 * @return boolean
	 */

	@SuppressWarnings("unchecked")
	public boolean checkDate(Date datum, Integer mit_id) {
		HibernateTemplate template = getHibernateTemplate();
		SimpleDateFormat formatDatum = new SimpleDateFormat("yyyy-MM-dd");
		String tmp = "'" + formatDatum.format(datum) + "'";
		List<Urlaub> results = template
				.find("from Urlaub u where u.urlaub_von <= " + tmp
						+ " AND u.urlaub_bis >= " + tmp
						+ " AND u.mitarbeiter.id = " + mit_id
						+ " AND u.urlaub_genehmigt = true");
		if (results.size() != 0)
			return true;
		else
			return false;
	}
}
